/*
 * @(#)LogonBOImpl.java 1.0 2012/03/06
 *
 * Copyright (c) 
 *
 * 
 */

package mock.ab_sd.business;

import java.util.ArrayList;

import mock.ab_sd.common.valueobject.UserVO;
import mock.ab_sd.daointerface.UserDAOImpl;

/**
 * LogonBOImpl class 
 * @author QuyGP
 * @created 15/8/2012
 */
public class LogonBOImpl implements LogonBO{

    private UserVO userLoged;
	/**
	 * authenticate whether user name and password match database
	 * @param userName
	 * @param password
	 * @return the result of authentication:
	 *      true is successful, false is unsuccessful
	 * @throws Exception
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public boolean authenticate(String userName, String password)
			throws Exception {
		
		// declare variable to store the result of authentication
		boolean result = false;
		
		// create an UserDAOImpl object
		UserDAOImpl userDAO = new UserDAOImpl();
		
		// create an ArrayList to contain UserVO objects
		ArrayList<UserVO> arrayListOfUser = userDAO.getArrayListOfUser();
		
		// to step through the elements in arrayListOfUser 
		for (UserVO user : arrayListOfUser) {
			
			/* check whether user name and password match database,
			 * check whether status is null and active status is true 
			 */
			if (user.getUserName().equals(userName)
					&& user.getPassword().equals(password)
					&& user.isActive() == true) {
				userLoged= user;
				result = true;
				
			}
		}
		return result; // return the result of authentication
	}
    /**
     * Set the value for user.
     * @param user the user to set
	 * @author QuyGP
	 * @created 15/8/2012
     */
    public void setUser(UserVO user) {
        this.userLoged = user;
    }
    /**
    * Get value of user.
    * @return the user
	* @author QuyGP
	* @created 15/8/2012
    */
    public UserVO getUser() {
        return userLoged;
    }
}
